package com.atguigu.gulimall.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.ware.vo.MergeVO;
import com.atguigu.gulimall.ware.vo.PurchaseDoneVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.ware.entity.PurchaseEntity;
import com.atguigu.gulimall.ware.service.PurchaseService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.annotation.Resource;


@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Resource
    private PurchaseService purchaseService;
    /*查询*/

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("ware:purchase:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询未领取的采购单 /ware/purchase/unreceive/list
     * 请求的参数无
     * 响应"page": {
     * "totalCount": 0,
     * "pageSize": 10,
     * "totalPage": 0,
     * "currPage": 1,
     * "list": [{
     * "id": 1,
     * "assigneeId": 1,
     * "assigneeName": "aa",
     * "phone": "123",
     * "priority": 1,
     * "status": 1,
     * "wareId": 1,
     * "amount": 22.0000,
     * "createTime": "2019-12-12",
     * "updateTime": "2019-12-12"
     * }]    * 	}
     */
    @RequestMapping("/unreceive/list")
    //@RequiresPermissions("ware:purchase:list")
    public R unReceiveList(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryPageUnreceivePurchase(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ware:purchase:info")
    public R info(@PathVariable("id") Long id) {
        PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }


    /*新增*/

    /**
     * 合并采购单 /ware/purchase/merge
     * 请求的参数:
     * purchaseId: 1, //整单id
     * items:[1,2,3,4] //合并项集合
     * 响应的数据
     * {
     * "msg": "success",
     * "code": 0
     * }
     */
    @PostMapping("/merge")
    //@RequiresPermissions("ware:purchase:save")
    public R merge(@RequestBody MergeVO mergeVO) {
        purchaseService.mergePurchase(mergeVO);
        return R.ok();
    }



    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ware:purchase:save")
    public R save(@RequestBody PurchaseEntity purchase) {
        purchase.setUpdateTime(new Date());
        purchase.setCreateTime(new Date());
        purchaseService.save(purchase);

        return R.ok();
    }

    /*删除*/

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ware:purchase:delete")
    public R delete(@RequestBody Long[] ids) {
        purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /*修改*/

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ware:purchase:update")
    public R update(@RequestBody PurchaseEntity purchase) {
        purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 领取采购单   ware/purchase/received
     * 请求的参数:采购单Id(是个数组)
     * 响应的数据：msg:success  code:0
     */
    @PostMapping("/received")
    //@RequiresPermissions("ware:purchase:save")
    public R received(@RequestBody List<Long> ids) {
        purchaseService.received(ids);
        return R.ok();
    }

    /**
     * 完成采购     post:/ware/purchase/done
     * 请求参数:
     * id: 123,//采购单id
     * items: [{itemId:1,status:4,reason:""}] itemId是采购项(采购需求id),status:是采购项的状态(3是已完成,4是采购失败),采购失败说原因
     * 响应结果:
     * 	"msg": "success",
     * 	"code": 0
     * 	我们有一个页面,展示的是该采购单里面的采购项(采购需求),哪些完成了会被勾中,点一个确认,勾中的就完成了,没完成的要指定没完成的原因
     * 	所以我们提交了每一个采购项(采购需求)的状态,勾上了就是采购完成了,没勾上还要提交原因
     */
    @PostMapping("/done")
    //@RequiresPermissions("ware:purchase:save")
    public R finish(@RequestBody PurchaseDoneVO doneVO) {
        purchaseService.done(doneVO);
        return R.ok();
    }
}
